package _case

import "fmt"

func FlowControlCase() {
	IfElseCase(0)
	IfElseCase(1)
	IfElseCase(2)
	fmt.Println("===============================")
	//forCase()
	SwitchCase("C", "C")
	fmt.Println("===============================")
	SwitchCase("A", 1)
	GotoCase()
}

func IfElseCase(a int) {
	if a == 0 {
		fmt.Println("执行 if 语句块")
	} else if a == 1 {
		fmt.Println("执行 else if 语句块")
	} else {
		fmt.Println("执行 else 语句块")
	}
}

func forCase() {
	for i := 0; i < 10; i++ {
		if i%2 == 0 {
			continue
		}
		fmt.Println("for1：执行for循环语句块 i =", i)
	}
	fmt.Println("===============================")

	var i1 int
	var b = true
	for b {
		i1++
		if i1 == 5 {
			b = false
		}
		fmt.Println("for2：执行for循环语句块 i =", i1)
	}
	fmt.Println("===============================")

	arr := [5]int{1, 2, 3, 4, 5}
	for k, v := range arr {
		fmt.Println(k, v)
	}
	fmt.Println("===============================")

	var i2 = 0
	for {
		i2++
		if i2 == 6 {
			break
		}
		fmt.Println("for3：执行for循环语句块 i =", i2)
	}
	fmt.Println("===============================")

lab:
	for i := 1; i < 10; i++ {
		for j := 1; j < 10; j++ {
			if i*j == 5 {
				break lab //跳出最外层循环
			}
			fmt.Println("for4：执行for循环语句块 i*j =", i*j)
		}
	}
	fmt.Println("===============================")

	str := "今天天气很好"
	for k, char := range str {
		fmt.Println(k, string(char))
	}
	fmt.Println("===============================")

}

func SwitchCase(alpha string, in interface{}) {
	switch alpha {
	case "A":
		fmt.Println("A")
	case "B":
		fmt.Println("B")
	case "C", "D":
		fmt.Println("C", "D")
		fallthrough
	case "E":
		fmt.Println("E")
	default:
		fmt.Println("default")
	}

	switch in.(type) {
	case string:
		fmt.Println("string")
	case int:
		fmt.Println("int")
	default:
		fmt.Println("default")
	}
}

func GotoCase() {
	var a = 0
label:
	for i := 0; i < 10; i++ {
		if a < 2 {
			a += 1
			fmt.Println("a =", a)
			goto label //跳转到lab位置，继续执行下面代码，如此会造成死循环
		}
	}
}
